package LeetCode.leetcode203;


public class Main {
    public static void main(String[] args) {
        ListNode node1;
        ListNode node2;
        ListNode node3;
        ListNode node4;
        ListNode node5;
        ListNode node6;
        ListNode node7;

        ListNode head = new ListNode();
        node1 = new ListNode(1);
        node2 = new ListNode(2);
        node3 = new ListNode(6);
        node4 = new ListNode(3);
        node5 = new ListNode(4);
        node6 = new ListNode(5);
        node7 = new ListNode(6);
        //头指针
        head.next = node1;

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        node7.next = null;
        System.out.println(removeElements(node1, 6));

    }


    public static ListNode removeElements(ListNode head, int val) {
        //虚拟头结点
        ListNode virtualNode = new ListNode();
        virtualNode.next = head;
        ListNode p = virtualNode;
        while (p.next != null) {
            if (p.next.val == val) {
                //删除这个节点
                p.next = p.next.next;
            } else {
                p = p.next;
            }
        }
        return virtualNode.next;
    }
    public  ListNode removeElements2(ListNode head, int val) {
        //虚拟头结点
        ListNode virtualNode = new ListNode();
        virtualNode.next = head;
        ListNode p = virtualNode;
        while (p.next != null){
            if (p.next.val == val)
                p.next = p.next.next;
            else
                p = p.next;
        }
        return virtualNode.next;
    }
}

